package com.example.demo.controller;

import com.example.demo.pojo.Back;
import com.example.demo.pojo.Borrow;
import com.example.demo.pojo.Result;
import com.example.demo.pojo.request.BookRequest;
import com.example.demo.pojo.request.BorrowRequest;
import com.example.demo.service.IBookService;
import com.example.demo.service.IBorrowService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/borrow")
public class BorrowControl {
    @Autowired
    private IBorrowService borrowService;
    //查询全部
    @GetMapping("/list")
    public Result list(){
        List<Borrow> select = borrowService.list();
        return Result.success(select);
    }

    //分页查询
    @GetMapping("/page")
    public Result page(BorrowRequest borrowRequest){
        Object page = borrowService.page(borrowRequest);
        return Result.success(page);
    }

    //添加数据
    @PostMapping("/add")
    public Result add(@RequestBody Borrow Borrow){
        borrowService.save(Borrow);
        return Result.success();
    }

    //根据id编辑回写
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        Borrow Borrow = borrowService.getUser(id);
        return Result.success(Borrow);
    }

    //根据id编辑
    @PutMapping("/update")
    public Result update(@RequestBody Borrow Borrow){
        borrowService.getIDUser(Borrow);
        return Result.success();
    }

    //根据id删除
    @DeleteMapping("/del/{id}")
    public Result delete(@PathVariable Integer id){
        borrowService.delete(id);
        return Result.success();
    }


    //根据id删除
    @DeleteMapping("/back/del/{id}")
    public Result deleteBack(@PathVariable Integer id){
        borrowService.deleteBack(id);
        return Result.success();
    }

    //还书分页查询
    @GetMapping("/back/page")
    public Result pageBack(BorrowRequest borrowRequest){
        Object page = borrowService.pageBack(borrowRequest);
        return Result.success(page);
    }

    //添加还书数据
    @PostMapping("/back/add")
    public Result addBack(@RequestBody Back back){
        borrowService.saveBack(back);
        return Result.success();
    }

    //首页图标接口
    @GetMapping("/lineCharts/{timeRange}")
    public Result lineCharts(@PathVariable String timeRange) {
        return Result.success(borrowService.getCountByTimeRange(timeRange));
    }
}
